#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>

using namespace std;

bool satisfiesConditions(vector<vector<int>>& grid) {
    int n = grid.size(), m = grid[0].size();
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
        {
            if (i + 1 < n && grid[i][j] != grid[i + 1][j])
                return false;
            if (j + 1 < m && grid[i][j] == grid[i][j + 1])
                return false;
        }

    return true;
}

struct ListNode {
    int val;
    ListNode* next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode* next) : val(x), next(next) {}
};

ListNode* rotateRight(ListNode* head, int k) {
    if (k == 0 || head == nullptr || head->next == nullptr)
        return head;
    int len = 1;
    ListNode* cur = head;
    while (cur->next)
    {
        len++;
        cur = cur->next;
    }

    int add = len - k % len;
    if (add == len)
        return head;
    cur->next = head;
    while (add--)
    {
        cur = cur->next;
    }
    ListNode* res = cur->next;
    cur->next = nullptr;
    return res;
}